using System;
using System.Web.Mvc;
using Limestone;
using LimestoneMVC.Models;

namespace LimestoneMVC.Controllers
{
    public class EditorController : Controller
    {

        public EditorController(IPageProvider pageProvider, ITemplateProvider templateProvider)
        {
            PageProvider = pageProvider;
            TemplateProvider = templateProvider;
        }

        protected IPageProvider PageProvider { get; set; }
        protected ITemplateProvider TemplateProvider { get; set; }

        public ActionResult Move(string pagePath, string cancelUrl)
        {
            var moveSettings = new EditPageModel
            {
                PagePath = pagePath,
                CancelUrl = cancelUrl
            };
            return View("MovePage", moveSettings);
        }

        public ActionResult Copy(string pagePath, string cancelUrl)
        {
            var copySettings = new EditPageModel
            {
                PagePath = pagePath,
                CancelUrl = cancelUrl
            };
            return View("CopyPage", copySettings);
        }

        public ActionResult Delete(string pagePath, string completeUrl)
        {
            PageProvider.Delete(pagePath);
            return Redirect(completeUrl);
        }


        public ActionResult Edit(string parentPagePath, string pagePath, string templatePath, string cancelUrl)
        {
            EditPageModel editSettings;
            var defaultName = "New-page";

            EditedPage page;

            // create new page
            if (string.IsNullOrEmpty(pagePath))
            {
                editSettings = new EditPageModel
                                   {
                                       ParentPagePath = parentPagePath,
                                       CancelUrl = cancelUrl,
                                       TemplatePath = templatePath
                                   };
                page = new EditedPage
                           {
                               Name = defaultName,
                               TemplatePath = templatePath,
                               Path = parentPagePath + "/" + defaultName,
                               OriginalName = ""
                           };
            }
            else  // edit existing page
            {
                var p = PageProvider.Get(pagePath);
                page = p.ToEditedPage();
                editSettings = new EditPageModel
                                   {
                                       ParentPagePath = parentPagePath,
                                       PagePath = pagePath,
                                       CancelUrl = cancelUrl,
                                       Page = page,
                                       TemplatePath = page.TemplatePath
                                   };
            }

            // creating new page but haven't selected a template yet
            if (string.IsNullOrEmpty(editSettings.TemplatePath))
            {
                ViewBag.Templates = TemplateProvider.GetAll();
                return View("SelectTemplate", editSettings);
            }

            TemplateProvider.SyncPageWithTemplate(page);
            TemplateProvider.AddFieldSettings(page);
            editSettings.Page = page;

            return View("Edit", editSettings);
        }


        [HttpPost]
        public ActionResult Save(EditPageModel editData)
        {
            if (!string.IsNullOrEmpty(editData.Page.OriginalName) && editData.Page.OriginalName != editData.Page.Name)
            {
                // TODO handle changed name
                // make sure the original page is moved or deleted
                // make sure we don't overwrite another existing page

                throw new Exception("name change not handled");
            }

            PageProvider.Save(editData.Page.ToIPage());

            return RedirectToAction("OpenPage", "Page", new { pagePath = editData.Page.Path.TrimStart('/') });
        }
    }
    public static class Extensions
    {

        public static IPage ToIPage(this EditedPage p)
        {
            return new Page
            {
                Created = p.Created,
                Deleted = p.Deleted,
                Hidden = p.Hidden,
                CreatedBy = p.CreatedBy,
                DeletedBy = p.DeletedBy,
                Fields = p.Fields,
                Modified = p.Modified,
                ModifiedBy = p.ModifiedBy,
                Name = p.Name,
                Path = p.Path,
                SortChildPagesBy = p.SortChildPagesBy,
                SortValue = p.SortValue,
                TemplatePath = p.TemplatePath,
                Title = p.Title
            };
        }
        public static EditedPage ToEditedPage(this IPage p)
        {
            return new EditedPage
            {
                Created = p.Created,
                Deleted = p.Deleted,
                Hidden = p.Hidden,
                CreatedBy = p.CreatedBy,
                DeletedBy = p.DeletedBy,
                Fields = p.Fields,
                Modified = p.Modified,
                ModifiedBy = p.ModifiedBy,
                Name = p.Name,
                Path = p.Path,
                SortChildPagesBy = p.SortChildPagesBy,
                SortValue = p.SortValue,
                TemplatePath = p.TemplatePath,
                Title = p.Title,

                OriginalName = p.Name
            };
        }
    }
}